<?php

namespace app\common\model;

use think\Model;

class Blacklist extends Model
{
    protected $autoWriteTimestamp = true;

    public function addBlack()
    {
        $param = request()->param();
        $blackId = $param['user_id'];
        $currentUserid = request()->userId;
        if($blackId == $currentUserid) TApiException('非法操作',50000,200);
        $arr = ['black_id' => $blackId,'user_id' => $currentUserid];
        // 已经存在记录
        if($this->where($arr)->find()) TApiException('对方已经被你拉黑过',40001,200);
        // 直接创建
        if(!$this->create($arr)) TApiException();
        return true;
    }

    public function removeBlack()
    {
        $param = request()->param();
        $blackId = $param['user_id'];
        $currentUserid = request()->userId;
        if($blackId == $currentUserid) TApiException('非法操作','50000',200);
        $arr = ['black_id' => $blackId,'user_id' => $currentUserid];
        $black = $this->where($arr)->find();
        if(!$black) TApiException('对方已不在你的黑名单内',40002,200);
        if(!$black->delete()) TApiException();
        return true;
    }
}
